/**
 * Tab页管理
 */

 /**
  * Tab对象
  */
function TabManager(){
    this.tab={};
    this.last=null;//最后一个
    this.current=null;//当前tab
    /**
     * {
     *     path:xxxx,meta:{title:xxxx}
     * 
     * }
     * 
     */
    this.add=(item,deleteAble = true)=>{
        if(this.last){
            var preTab=this.last; 
            var addTab = { preTab: preTab, tabItem: item, nextTab: null, deleteAble: deleteAble }
            this.tab[item.meta.title] = addTab; 
            this.last.nextTab = addTab
            this.current = addTab
            this.last = addTab

            return this.tab
        }else{
            var addTab = { preTab: null, tabItem: item, nextTab: null, deleteAble: deleteAble }
            this.tab[item.meta.title] = addTab; 
            this.last = addTab
            this.current = addTab
            this.last=addTab
            return this.tab
        }       
    }
    //删除tab页
    this.remove=(title)=>{
        var t = this.tab[title]
        if (!t){
            return this.tab
        }
        if (!t.deleteAble){
            return this.tab
        }
        //如果是最后一个
        if (!t.nextTab){
            t.preTab.nextTab=null
            this.current = t.preTab
            delete this.tab[title]
            return this.tab
        }
        var p = t.preTab, n = t.nextTab
        n.preTab=p
        p.nextTab=n
        this.current = p
        delete this.tab[title]
        return this.tab
    }

}
export default new TabManager()